<?php
/**
 * author：wang
 * mail:970633164@qq.com
 */
define("PHP_IN",true);
require "include/init.php";
require ROOT_PATH."smarty_connection.php";

if(!empty($_POST) && $_POST['dosubmit']){
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);
    $imgcode = trim($_POST['imgcode']);
    session_start();
    if(!isset($_SESSION['imgcode']) || $imgcode != $_SESSION['imgcode']){
        alert("验证码错误！");
        exit;
    }

    if($username==""){
        alert("用户名不能为空");
        exit;
    }
    if(strlen($username)<4 || strlen($username)>15){
        alert("用户名长度为4-15个字符");
        exit;
    }
    $match = preg_match("/^[a-z][a-z0-9]{4,14}$/i",$username);
    if(!$match){
        alert("用户名只能为英文字母开始的英文字母、数字任意组合");
        exit;
    }
    if(in_array($username,$user)){
        alert("您输入的用户名已存在");
        exit;
    }
    if($password == ""){
        alert("密码不能为空");
        exit;
    }
    if(strlen($password)<6 || strlen($password)>15){
        alert("密码长度为6-15个字符");
        exit;
    }
    $pwd_md5 = md5($password);


    $sql = "INSERT INTO user (username,password) VALUES ('$username','$pwd_md5')";
    $result = $db->query($sql);
    if($result){
        alert("注册成功","user_login.php");
    }else{
        alert("注册失败，请填写正确的注册信息");
    }
}

$smarty->display("user_reg.tpl");
$db->close();
?>
